package c.c.b.b.d;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.widget.ImageView.ScaleType;
import c.c.b.b.f.c;
import c.c.b.b.f.i;
import c.c.b.b.f.m;
import c.c.b.b.f.p;
import c.c.b.b.f.p.a;
import c.c.b.b.f.q;
import c.c.b.b.h.f;

/* compiled from: ImageRequest */
public class l extends c<Bitmap> {
    public static final Object D = new Object();
    public final int A;
    public final int B;
    public final ScaleType C;
    public final Object x = new Object();
    public a<Bitmap> y;
    public final Config z;

    public l(String str, a<Bitmap> aVar, int i, int i2, ScaleType scaleType, Config config) {
        super(0, str, aVar);
        setRetryPolicy(new i(1000, 2, 2.0f));
        this.y = aVar;
        this.z = config;
        this.A = i;
        this.B = i2;
        this.C = scaleType;
        setShouldCache(false);
    }

    public static int a(int i, int i2, int i3, int i4, ScaleType scaleType) {
        if (i == 0 && i2 == 0) {
            return i3;
        }
        if (scaleType == ScaleType.FIT_XY) {
            return i == 0 ? i3 : i;
        } else {
            if (i == 0) {
                return (int) (((double) i3) * (((double) i2) / ((double) i4)));
            } else if (i2 == 0) {
                return i;
            } else {
                double d = ((double) i4) / ((double) i3);
                double d2;
                if (scaleType == ScaleType.CENTER_CROP) {
                    d2 = (double) i2;
                    if (((double) i) * d < d2) {
                        i = (int) (d2 / d);
                    }
                    return i;
                }
                d2 = (double) i2;
                if (((double) i) * d > d2) {
                    i = (int) (d2 / d);
                }
                return i;
            }
        }
    }

    public final p<Bitmap> b(m mVar) {
        Object decodeByteArray;
        byte[] bArr = mVar.b;
        Options options = new Options();
        if (this.A == 0 && this.B == 0) {
            options.inPreferredConfig = this.z;
            decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
        } else {
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            int i = options.outWidth;
            int i2 = options.outHeight;
            int a = a(this.A, this.B, i, i2, this.C);
            int a2 = a(this.B, this.A, i2, i, this.C);
            options.inJustDecodeBounds = false;
            float f = 1.0f;
            while (true) {
                float f2 = 2.0f * f;
                if (((double) f2) > Math.min(((double) i) / ((double) a), ((double) i2) / ((double) a2))) {
                    break;
                }
                f = f2;
            }
            options.inSampleSize = (int) f;
            decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            if (decodeByteArray != null && (decodeByteArray.getWidth() > a || decodeByteArray.getHeight() > a2)) {
                Bitmap createScaledBitmap = Bitmap.createScaledBitmap(decodeByteArray, a, a2, true);
                decodeByteArray.recycle();
                decodeByteArray = createScaledBitmap;
            }
        }
        if (decodeByteArray == null) {
            return new p(new f(mVar));
        }
        return new p(decodeByteArray, b.i.b.f.f(mVar));
    }

    public void cancel() {
        super.cancel();
        synchronized (this.x) {
            this.y = null;
        }
    }

    public c.c getPriority() {
        return c.c.LOW;
    }

    public p<Bitmap> a(m mVar) {
        synchronized (D) {
            try {
                mVar = b(mVar);
            } catch (OutOfMemoryError e) {
                q.d("Caught OOM for %d byte image, url=%s", Integer.valueOf(mVar.b.length), getUrl());
                return new p(new f(e));
            } catch (Throwable th) {
            }
        }
        return mVar;
    }

    public void a(p<Bitmap> pVar) {
        a aVar;
        synchronized (this.x) {
            aVar = this.y;
        }
        if (aVar != null) {
            aVar.a(pVar);
        }
    }
}
